package org.jeecg.modules.workflow.convert.acct.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boc.ewr.bcimp.entity.CaRtnList;
import com.boc.ewr.bcimp.entity.Cac;
import com.boc.ewr.bcimp.entity.Pro;
import com.boc.ewr.bcimp.service.ICaRtnListService;
import com.boc.ewr.bcimp.service.ICacService;
import com.boc.ewr.bcimp.service.IProService;
import com.boc.ewr.business.entity.ElmtbAssmtApply;
import com.sun.corba.se.spi.ior.iiop.IIOPFactories;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.SysDepartModel;
import org.jeecg.common.workflow.vo.acct.elmtbAssmt.BpmAcctElmtbAssmtBaseVo;
import org.jeecg.common.workflow.vo.acct.elmtbAssmt.BpmAcctElmtbAssmtRespVo;
import org.jeecg.modules.workflow.convert.acct.BpmAcctElmtbAssmtConvert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : Wzz
 * @create : 2023-04- 27- 14:25
 * @desc :
 */
@Service
public class BpmAcctElmtbAssmtConvertImpl implements BpmAcctElmtbAssmtConvert {

    @Autowired
    private IProService proService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private ICacService cacService;


    @Override
    public ElmtbAssmtApply convert(BpmAcctElmtbAssmtBaseVo bean) {
        if (bean == null) {
            return null;
        }
        ElmtbAssmtApply elmtbAssmtApply = new ElmtbAssmtApply();
        BeanUtils.copyProperties(bean, elmtbAssmtApply);
        elmtbAssmtApply.setApplyDate(new Date());

        return elmtbAssmtApply;
    }

    @Override
    public BpmAcctElmtbAssmtRespVo convert(ElmtbAssmtApply bean) {
        if (bean == null) {
            return null;
        }
        BpmAcctElmtbAssmtRespVo bpmAcctElmtbAssmtRespVo = new BpmAcctElmtbAssmtRespVo();

        BeanUtils.copyProperties(bean, bpmAcctElmtbAssmtRespVo);
        bpmAcctElmtbAssmtRespVo.setCreateTime(bean.getApplyDate());

        return bpmAcctElmtbAssmtRespVo;
    }

    @Override
    public BpmAcctElmtbAssmtRespVo getInfoByProtocolNo(String bean) {
        BpmAcctElmtbAssmtRespVo bpmAcctElmtbAssmtRespVo = new BpmAcctElmtbAssmtRespVo();

        List<Pro> list = proService.list(
                new LambdaQueryWrapper<Pro>()
                        .eq(Pro::getCaProtocolNo, bean)
        );

        if (list.size() > 0) {
            Pro pro = list.get(0);

            try {
                SysDepartModel sysDepartModel = sysBaseAPI.selectAllById(pro.getSignedOrg());
                bpmAcctElmtbAssmtRespVo.setAdminOrgNo(sysDepartModel.getParentId());
                bpmAcctElmtbAssmtRespVo.setOrgBranchNo(sysDepartModel.getOrgCode());
                bpmAcctElmtbAssmtRespVo.setOrgBranchNm(sysDepartModel.getDepartName());
            } catch (Exception e) {
                bpmAcctElmtbAssmtRespVo.setAdminOrgNo("000000");
                bpmAcctElmtbAssmtRespVo.setOrgBranchNo("000000");
                bpmAcctElmtbAssmtRespVo.setOrgBranchNm("机构Id不存在，查不到机构名称");
            }


            bpmAcctElmtbAssmtRespVo.setCusmNo(pro.getCusmNo());
            bpmAcctElmtbAssmtRespVo.setCusmNm(pro.getCusmName());

            // 协议性质 cac中的协议标识
            List<Cac> cacList = cacService.list(
                    new LambdaQueryWrapper<Cac>()
                            .eq(Cac::getCaProtocolNo, bean)
            );
            if (cacList.size()>0) {
                Cac cac = cacList.get(0);
                bpmAcctElmtbAssmtRespVo.setProtocolType(cac.getIskey());
            }
        }

        return bpmAcctElmtbAssmtRespVo;
    }

    @Override
    public List<BpmAcctElmtbAssmtRespVo> convertList(List<ElmtbAssmtApply> list) {
        if (list == null) {
            return null;
        }

        List<BpmAcctElmtbAssmtRespVo> list1 = new ArrayList<BpmAcctElmtbAssmtRespVo>(list.size());

        for (ElmtbAssmtApply elmtbAssmtApply : list) {
            list1.add(convert(elmtbAssmtApply));
        }

        return list1;
    }

    @Override
    public IPage<BpmAcctElmtbAssmtRespVo> convertPage(IPage<ElmtbAssmtApply> page) {
        if (page == null) {
            return null;
        }

        IPage<BpmAcctElmtbAssmtRespVo> pageResult = new Page<>();

        pageResult.setRecords(convertList(page.getRecords()));
        pageResult.setTotal(page.getTotal());

        return pageResult;
    }
}
